﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using Flurl;
using Newtonsoft.Json;
using ZilLion.Core.ConfigManager.Manager;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.TaskManager.Entity;
using ZilLion.TaskManager.Entity.Config;
using ZilLion.TaskManager.Entity.Entity;
using ZilLion.TaskManager.Entity.Response;

namespace ZilLion.TaskManager.Repository
{
    public class TaskModuleConfigRespository : ITaskModuleConfigRespository

    {
        public TaskModuleConfigResponse GetTaskModuleConfig()
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskConfig", "GetAllTaskModuleConfig"
            );
            return HttpHelper.HttpRequestGet<TaskModuleConfigResponse>($"{url} ");
        }

        public TaskModuleConfigResponse GetTaskModuleConfigById(string moduleid)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskConfig", "GetTaskModuleConfig"
            );
            return HttpHelper.HttpRequestGet<TaskModuleConfigResponse>($"{url}?moduleid={moduleid} ");
        }

        public UploadTaskPackageResponse UpLoadPackageFile(string filepath)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "UploadTaskPackage", "Upload"
            );
            Func<HttpContent, string> getStrFunc = content => content.ReadAsStringAsync().Result;
            return HttpHelper.UpLoadFile<UploadTaskPackageResponse>($"{url} ", filepath, getStrFunc);
        }

        public bool SaveTaskModuleConfig(TaskModuleConfigEntity entity)
        {
            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "TaskConfig", "SaveModuleConfig"
            );


            var httpcontent = new StringContent(JsonConvert.SerializeObject(entity))
            {
                Headers = {ContentType = new MediaTypeHeaderValue("application/json")}
            };


            var response = HttpHelper.HttpRequestPost<ResponseBase>($"{url} ", httpcontent);
            return response != null && response.IsSuccess;
        }
    }
}